package com.gobang.controller;

import com.gobang.domain.dto.ScoreUpdateRequestDTO;
import com.gobang.domain.vo.*;
import com.gobang.service.ScoreService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 积分管理控制器
 * 提供积分更新和积分流水查询的HTTP接口
 * 
 * @author System
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/scores")
@RequiredArgsConstructor
@Validated
public class ScoreController {

    private final ScoreService scoreService;

    /**
     * 更新用户积分
     * 前端不会主动调用该接口
     *
     * @param request 积分更新请求数据
     * @return 积分更新结果
     */
//    @PostMapping("/update")
//    public Result<ScoreUpdateResponseVO> updateUserScore(@Valid @RequestBody ScoreUpdateRequestDTO request) {
//        log.info("接收积分更新请求: userId={}, gameResult={}", request.getUserId(), request.getGameResult());
//
//        try {
//            ScoreUpdateResponseVO response = scoreService.updateUserScore(request);
//            log.info("积分更新成功: userId={}, newScore={}", request.getUserId(), response.getNewScore());
//            return Result.success(response, "积分更新成功");
//        } catch (Exception e) {
//            log.error("积分更新失败: userId={}, error={}", request.getUserId(), e.getMessage(), e);
//            throw e;
//        }
//    }

    /**
     * 查询用户积分流水记录
     * 支持分页查询，按游戏时间倒序排列
     *
     * @param userId 用户ID
     * @param current 当前页码，默认为1
     * @param size 每页大小，默认为10
     * @return 积分流水记录分页数据
     */
    @GetMapping("/records/{userId}")
    // public Result<List<ScoreRecordVO>> getScoreRecords(
    public Result<ScoreRecordQueryVO> getScoreRecords(
            @PathVariable @NotNull(message = "用户ID不能为空") Long userId,
            @RequestParam(defaultValue = "1") @Positive(message = "页码必须为正整数") Integer current,
            @RequestParam(defaultValue = "10") @Positive(message = "每页大小必须为正整数") Integer size) {
        
        log.debug("接收积分流水查询请求: userId={}, current={}, size={}", userId, current, size);
        
        try {
            // 方式二：将翻页结果全部保存到 Data 里面
             ScoreRecordQueryVO response = scoreService.getScoreRecordsByUserId(userId, current, size);
             log.debug("积分流水查询成功: userId={}, recordCount={}", userId, response.getRecords().size());
             return Result.success(response, "积分流水查询成功");

            // 方式1：按我们设计的Result,翻页使用方法如下：
//            ScoreRecordQueryVO response = scoreService.getScoreRecordsByUserId(userId, current, size);
//            List<ScoreRecordVO> list = response.getRecords();
//            PageInfo pageInfo = new PageInfo(
//                    Long.parseLong(String.valueOf(response.getCurrent())),
//                    Long.parseLong(String.valueOf(response.getSize())),
//                    response.getTotal());
//            return Result.success(list, "积分流水查询成功").withPage(pageInfo);
        } catch (Exception e) {
            log.error("积分流水查询失败: userId={}, error={}", userId, e.getMessage(), e);
            throw e;
        }
    }
} 